/*
 * Project Name: jmax
 * File Name: ClientMonitor.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.hotel.monitor;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.hotel.protocol.vo.RCUDevice;

/**
 * 
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-4-23 上午9:40:53
 */
public class ClientMonitor
{
	private static Log log = LogFactory.getLog(ClientMonitor.class);
	private static Map<String, RCUDeviceRtClient> devRTClientMap = new ConcurrentHashMap<String, RCUDeviceRtClient>();
	
	private static Map<String, DeviceMonitorClient> deviceMonitorClientMap = new ConcurrentHashMap<String, DeviceMonitorClient>();
	
	private static Map<String, RoomMonitorClient> roomMonitorClientMap = new ConcurrentHashMap<String, RoomMonitorClient>();

	
	private static Thread checkClientThread = new Thread(new CheckClientRunning(),"CheckClientThread");//心跳线程

	public static Map<String, RCUDeviceRtClient> getDevRTClientMap()
	{
		return devRTClientMap;
	}



	/**
	 * 
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-4-23 上午9:55:55
	 * @param clientId
	 */
	public static List<RCURunEvent> loadRtEvent(String clientId)
	{
		RCUDeviceRtClient rtClient = null;
		if (!devRTClientMap.containsKey(clientId))
		{
			rtClient = new RCUDeviceRtClient(clientId);
			devRTClientMap.put(clientId, rtClient);
		}else
		{
			rtClient = devRTClientMap.get(clientId);
		}
		//校验客户端线程
		if(!checkClientThread.isAlive()&&devRTClientMap.size() > 0)
		{
			if(checkClientThread.getState() == Thread.State.TERMINATED)//如果该线程是否已经结束，则建立新的线程
			{
				checkClientThread = new Thread(new CheckClientRunning(),"CheckClientThread");
			}
			checkClientThread.start();
		}
		return rtClient.clientGetAccTransactionList();
	}
	
	/**
	 * 设备监控记录获取
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-4-24 下午2:59:41
	 * @param clientId
	 * @return
	 */
	public static List<RCUDevice> loadRcuDevice(String clientId)
	{
		DeviceMonitorClient deviceMonitorClient = null;
		if (!deviceMonitorClientMap.containsKey(clientId))
		{
			deviceMonitorClient = new DeviceMonitorClient(clientId);
			//首次客户请求直接获取全部
			deviceMonitorClient.loadAllDeviceMonitors();
			deviceMonitorClientMap.put(clientId, deviceMonitorClient);
		}else
		{
			deviceMonitorClient = deviceMonitorClientMap.get(clientId);
		}
		//校验客户端线程
		if(!checkClientThread.isAlive()&&deviceMonitorClientMap.size() > 0)
		{
			if(checkClientThread.getState() == Thread.State.TERMINATED)//如果该线程是否已经结束，则建立新的线程
			{
				checkClientThread = new Thread(new CheckClientRunning(),"CheckClientThread");
			}
			checkClientThread.start();
		}
		return deviceMonitorClient.clientGetRcuDevices();
	}
	
	
	/**
	 * 给在线的客户端添加设备状态添加监控数据
	 * 
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-4-24 下午3:54:20
	 * @param rcuDevice
	 */
	public static void addDeviceMonitorData(RCUDevice rcuDevice)
	{
		for (String clientId : deviceMonitorClientMap.keySet())
		{
    		DeviceMonitorClient client = deviceMonitorClientMap.get(clientId);
    		if (null != client)
			{
    			client.getRcuDevices().add(rcuDevice);
			}
		}
	}
	
	/**
	 * 房间监控记录获取
	 * @author max.zheng
	 * @since 2015-4-24 下午2:59:41
	 * @param clientId
	 * @return
	 */
	public static List<RoomMonitorInfo> loadRoomInfo(String clientId)
	{
		RoomMonitorClient roomMonitorClient = null;
		if (!roomMonitorClientMap.containsKey(clientId))
		{
			roomMonitorClient = new RoomMonitorClient(clientId);
			//首次客户请求直接获取全部
			roomMonitorClient.loadAllRoomInfos();
			roomMonitorClientMap.put(clientId, roomMonitorClient);
		}else
		{
			roomMonitorClient = roomMonitorClientMap.get(clientId);
		}
		//校验客户端线程
		if(!checkClientThread.isAlive()&&roomMonitorClientMap.size() > 0)
		{
			if(checkClientThread.getState() == Thread.State.TERMINATED)//如果该线程是否已经结束，则建立新的线程
			{
				checkClientThread = new Thread(new CheckClientRunning(),"CheckClientThread");
			}
			checkClientThread.start();
		}
		return roomMonitorClient.clientGetRoomMonitorInfos();
	}
	
	
	/**
	 * 给在线的客户端添加客房监控数据
	 * 
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-4-24 下午3:54:20
	 * @param rcuDevice
	 */
	public static void addRoomMonitorData(RoomMonitorInfo roomMonitorInfo)
	{
		for (String clientId : roomMonitorClientMap.keySet())
		{
    		RoomMonitorClient client = roomMonitorClientMap.get(clientId);
    		if (null != client)
			{
    			client.getRoomMonitorInfos().add(roomMonitorInfo);
			}
		}
	}
	/**
	 * 
	 * 客户端是否过期检验判断
	 * @version TODO 添加版本
	 * @see 相关类或方法，不需要请删除此行
	 * @since 2015-4-24 下午3:51:31
	 */
	private static class CheckClientRunning implements Runnable
	{
		public void run()
		{
			try
			{
			    do
	            {
			    	//事件客户端过期监控
			    	for (String clientId : devRTClientMap.keySet())
					{
			    		RCUDeviceRtClient client = devRTClientMap.get(clientId);
			    		if (null != client)
						{
			    			if(System.currentTimeMillis() - client.getLastAccessedTime() > 1000 * 30)//超过半分钟没有更新则移动掉该client
	                        {
			    				devRTClientMap.remove(clientId);//循环里面不可以进行remove，所以先保存起来
	                        }
						}
					}
			    	//设备监控客户端过期监控
			    	for (String clientId : deviceMonitorClientMap.keySet())
					{
			    		DeviceMonitorClient client = deviceMonitorClientMap.get(clientId);
			    		if (null != client)
						{
			    			if(System.currentTimeMillis() - client.getLastAccessedTime() > 1000 * 30)//超过半分钟没有更新则移动掉该client
	                        {
			    				deviceMonitorClientMap.remove(clientId);//循环里面不可以进行remove，所以先保存起来
	                        }
						}
					}
			    	//设备监控客户端过期监控
			    	for (String clientId : roomMonitorClientMap.keySet())
					{
			    		RoomMonitorClient client = roomMonitorClientMap.get(clientId);
			    		if (null != client)
						{
			    			if(System.currentTimeMillis() - client.getLastAccessedTime() > 1000 * 8)//超过半分钟没有更新则移动掉该client
	                        {
			    				roomMonitorClientMap.remove(clientId);//循环里面不可以进行remove，所以先保存起来
	                        }
						}
					}
	                Thread.sleep(30000);
	            } while(devRTClientMap.size() > 0||deviceMonitorClientMap.size()>0||roomMonitorClientMap.size()>0);//无客户端时结束线程
			}
			catch (Exception e)
			{
				log.error("CheckClientRunning", e);
			}
			
		}
		
	}





	

	
}

